﻿using System;
using System.Linq;
using System.Windows.Media;
using CCWP7.Model;
using System.Diagnostics;
using Caliburn.Micro;

namespace CCWP7.CaliburnMicro
{
    public class ProjectViewModel : PropertyChangedBase
    {
        readonly IProjectReport _projectReport;

        public ProjectViewModel(IProjectReport projectReport)
        {
            Debug.Assert(projectReport != null, "projectReport is null.");

            _projectReport = projectReport;

            SetProject(projectReport);
        }

        void SetProject(IProjectReport projectReport)
        {
            Activity = projectReport.Activity;
            BuildStage = projectReport.BuildStage;
            Category = projectReport.Category;
            LastBuildLabel = projectReport.LastBuildLabel;
            LastBuildStatus = projectReport.LastBuildStatus;
            LastBuildTime = projectReport.LastBuildTime;
            Name = projectReport.Name;
            NextBuildTime = projectReport.NextBuildTime;
            ServerName = projectReport.ServerName;
            Status = projectReport.Status;
            WebUrl = projectReport.WebUrl;

            Refresh();
        }

        public string LastBuildStatusImageUri
        {
            get
            {
                switch (LastBuildStatus)
                {
                    case "Success":
                        return "/icons/Green.png";
                    case "Failure":
                        return "/icons/Red.png";
                    case "Exception":
                        return "/icons/Orange.png";
                    case "Cancelled":
                        return "/icons/Yellow.png";
                    default:
                    case "Unknown":
                        return "/icons/Gray.png";
                }
            }
        }

        ImageSourceConverter s_converter = new ImageSourceConverter();
        public ImageSource LastBuildStatusImage
        {
            get
            {
                return (ImageSource) s_converter.ConvertFromString(LastBuildStatusImageUri);
            }
        }

        public string PageTitle
        {
            get
            {
                return Name;
            }
        }

        public string Activity { get; private set; }
        public string BuildStage { get; private set; }
        public string Category { get; private set; }
        public string LastBuildLabel { get; private set; }
        public string LastBuildStatus { get; private set; }
        public DateTime LastBuildTime { get; private set; }
        public string Name { get; private set; }
        public DateTime NextBuildTime { get; private set; }
        public string ServerName { get; private set; }
        public string Status { get; private set; }
        public string WebUrl { get; private set; }

        public override int GetHashCode()
        {
            return _projectReport.GetHashCode();
        }
    }
}
